import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcome from '../components/welcome'
import Users from '../components/user/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: login },
  {
    path: '/home',
     component: home,
     redirect: '/welcome',
     children: [{ path: '/welcome', component: welcome },
     { path: '/users', component: Users },
     { path: '/rights', component: Rights },
     { path: '/roles', component: Roles },
     { path: '/categories', component: Cate },
     { path: '/params', component: Params }

     ]
    }
]

const router = new VueRouter({
  routes,
  mode: 'history'
})
// 路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  // next('/login)表示强制跳转到login
  if (!tokenStr) return next('/login')
  next()
})

export default router
